﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using QLVMBAYDatacess;
using QLVMBAYDomain;

namespace QLVMBAY.Admin
{
    public partial class QLThanhPho : System.Web.UI.Page
    {
        static bool Insert = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadGrid();
            }
        }
        private void LoadGrid()
        {
            grdThanhPho.DataSource = ThanhPhoDA.Inst.GetAll(true);            
            grdThanhPho.DataBind();
            if (grdThanhPho.PageCount <= 1)
            {
                grdThanhPho.PagerStyle.Visible = false;
            }
        }
        
        private void Load_ListQG()
        {            
            List<QuocGia> list = new List<QuocGia>();
            list = QuocGiaDA.Inst.GetAll(false);
            DropQG.DataSource = list;
            DropQG.DataTextField = "TenQG";
            DropQG.DataValueField = "MaQG";
            DropQG.DataBind();            
            DropQG.Items.Insert(0,new ListItem("Chọn", "0"));  
        }
        private void ResetControlValues()
        {
            txtTenTP.Text = "";
            txtMaTP.Text = "";
            DropQG.SelectedIndex = -1;
        }
        protected void AddButton_Click(object sender, EventArgs e)
        {
            pnUpdate.Visible = true;            
            pnView.Visible = false;
            ResetControlValues();
            Insert = true;
            Load_ListQG();
        }

        protected void RefreshButton_Click(object sender, EventArgs e)
        {
            LoadGrid();
        }

        protected void Update_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                ThanhPho TP = new ThanhPho();
                TP.MaThanhPho = txtMaTP.Text;
                TP.TenThanhPho = txtTenTP.Text;                
                TP.ItemQuocGia = new QuocGia() { MaQG = DropQG.SelectedValue };
                TP.IsMacDinh = 0;                
                if (Insert == true)
                {
                    ThanhPhoDA.Inst.Insert(TP);
                }
                else
                {
                    ThanhPhoDA.Inst.Update(TP);
                }
                LoadGrid();
                pnView.Visible = true;
                pnUpdate.Visible = false;
                Insert = false;
            }
        }

        protected void Back_Click(object sender, EventArgs e)
        {
            pnView.Visible = true;
            pnUpdate.Visible = false;
            Insert = false;
        }

        protected void grdThanhPho_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            var itemTP = ThanhPhoDA.Inst.GetByPrimaryKey(false, e.Item.Cells[0].Text);
            switch (e.CommandName)
            {
                case "Edit":
                    Insert = false;
                    txtMaTP.Text = itemTP.MaThanhPho.ToString();
                    txtMaTP.ReadOnly = true;
                    txtTenTP.Text = itemTP.TenThanhPho.ToString();                    
                    Load_ListQG();
                    DropQG.SelectedValue = itemTP.ItemQuocGia.MaQG;                        
                    pnView.Visible = false;
                    pnUpdate.Visible = true;
                    break;

                case "Delete":
                    ThanhPhoDA.Inst.Delete(itemTP);
                    LoadGrid();
                    break;
            }
        }

        protected void grdThanhPho_ItemDataBound(object sender, DataGridItemEventArgs e)
        {           
            if (e.Item.ItemIndex != -1)
            {
                var QG = (ThanhPho)e.Item.DataItem;
                var lbl = e.Item.FindControl("lblTenQG") as Label;
                lbl.Text = QG.ItemQuocGia.TenQG;
            }
        }

        protected void grdThanhPho_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            grdThanhPho.CurrentPageIndex = e.NewPageIndex;
            LoadGrid();
        }
    }
}